using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.CompanyDomain;
//using ShivamEMR.Web.Areas.Company.Models;
using ShivamEMR.Model.DataInterfaces.CompanyDataInterf;
using ShivamEMR.Model;
using ShivamEMR.Web.Models;

namespace ShivamEMR.Web.Areas.Company.Controllers
{   
    public class BranchController : Controller
    {
		private readonly IBranchGroupRepository branchgroupRepository;
		private readonly IBranchRepository branchRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public BranchController() 
        {
            branchgroupRepository = Registry.RepositoryFactory.GetBranchGroupRepository();
            branchRepository = Registry.RepositoryFactory.GetBranchRepository();
        }

        public BranchController(IBranchGroupRepository branchgroupRepository, IBranchRepository branchRepository)
        {
			this.branchgroupRepository = branchgroupRepository;
			this.branchRepository = branchRepository;
        }

        //
        // GET: /Branch/
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Index()
        {
            //return View(branchRepository.AllIncluding(branch => branch.Addresses, branch => branch.Contacts, branch => branch.Employees, branch => branch.Departments));
            return View(branchRepository.All);
        }

        //
        // GET: /Branch/Details/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Details(int id)
        {
            return View(branchRepository.Find(id));
        }

        //
        // GET: /Branch/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Create()
        {
			ViewBag.PossibleBranchGroups = branchgroupRepository.All;
            return View();
        } 

        //
        // POST: /Branch/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Create(Branch branch)
        {
            if (ModelState.IsValid) {
                branchRepository.Create(branch);
                branchRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleBranchGroups = branchgroupRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Branch/Edit/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleBranchGroups = branchgroupRepository.All;
             return View(branchRepository.Find(id));
        }

        //
        // POST: /Branch/Edit/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Edit(Branch branch)
        {
            if (ModelState.IsValid) {
                branchRepository.Update(branch);
                branchRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleBranchGroups = branchgroupRepository.All;
				return View();
			}
        }

        //
        // GET: /Branch/Delete/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Delete(int id)
        {
            return View(branchRepository.Find(id));
        }

        //
        // POST: /Branch/Delete/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            //branchRepository.Delete(id);
          var branch=  branchRepository.Find(id);
          branchRepository.Delete(branch);
            branchRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

